﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace 桌面视频录制
{
    [Serializable]
    public class DrawLine : DrawObject
    {
        protected Point startPoint;
        protected Point endPoint;
        protected int whichControlRegion;//从startPoint到endPoint 0 1 2
        protected Point middleLeftPoint;
        protected Point middleRightPoint;
        protected bool isLeftEnabled;
        protected bool isRightEnabled;

        public DrawLine() { }
        public DrawLine(Point startPoint, Point endPoint)
        {
            this.startPoint = startPoint;
            this.endPoint = endPoint;
            Point middlePoint = new Point((startPoint.X + endPoint.X) / 2, (startPoint.Y + endPoint.Y) / 2);
            this.middleLeftPoint = new Point((startPoint.X + middlePoint.X) / 2, (startPoint.Y + middlePoint.Y) / 2);
            this.middleRightPoint = new Point((endPoint.X + middlePoint.X) / 2, (endPoint.Y + middlePoint.Y) / 2);
        }
        public override void Draw(Graphics g)
        {
            Pen pen = new Pen(this.PenColor);
            pen.Width = this.PenWidth;
            if (isLeftEnabled == false && isRightEnabled == false)
                g.DrawLine(pen, startPoint, endPoint);
            else if (isLeftEnabled && !isRightEnabled)
                g.DrawCurve(pen, new Point[] { startPoint, middleLeftPoint, endPoint });
            else if (!isLeftEnabled && isRightEnabled)
                g.DrawCurve(pen, new Point[] { startPoint, middleRightPoint, endPoint });
            else if (isLeftEnabled && isRightEnabled)
                g.DrawCurve(pen, new Point[] { startPoint, middleLeftPoint, middleRightPoint, endPoint });
            pen.Dispose();
        }
        public override void MouseMove(MouseEventArgs e)
        {
            this.endPoint = new Point(e.X, e.Y);
            Point middlePoint = new Point((startPoint.X + endPoint.X) / 2, (startPoint.Y + endPoint.Y) / 2);
            this.middleLeftPoint = new Point((startPoint.X + middlePoint.X) / 2, (startPoint.Y + middlePoint.Y) / 2);
            this.middleRightPoint = new Point((endPoint.X + middlePoint.X) / 2, (endPoint.Y + middlePoint.Y) / 2);
        }
        public override bool JudgeIsSelected(MouseEventArgs e)
        {
            int angle1 = (int)(Math.Atan2(e.Y - startPoint.Y, e.X - startPoint.X) * 180 / Math.PI);
            int angle2 = (int)(Math.Atan2(endPoint.Y - e.Y, endPoint.X - e.X) * 180 / Math.PI);
            if ((angle1 - angle2) > -5 && (angle1 - angle2) < 5)
            {
                return true;
            }
            return false;
        }
        public override void DrawControlPoint(Graphics g)
        {
            StaticFunction.DrawControlRegion(g, startPoint);
            StaticFunction.DrawControlRegion(g, middleLeftPoint);
            StaticFunction.DrawControlRegion(g, middleRightPoint);
            StaticFunction.DrawControlRegion(g, endPoint);

            Pen pen = new Pen(this.PenColor);
            pen.Width = this.PenWidth;
            pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
            pen.DashPattern = new float[] { 5.0f, 3.0f };
            g.DrawLine(pen, startPoint, endPoint);
            pen.Dispose();
        }
        public override void MoveLocation(MouseEventArgs e)
        {
            int dx = e.X - this.MouseDownPoint.X;
            int dy = e.Y - this.MouseDownPoint.Y;
            this.MouseDownPoint = new Point(e.X, e.Y);
            this.startPoint.X += dx;
            this.startPoint.Y += dy;
            this.endPoint.X += dx;
            this.endPoint.Y += dy;
            this.middleLeftPoint.X += dx;
            this.middleLeftPoint.Y += dy;
            this.middleRightPoint.X += dx;
            this.middleRightPoint.Y += dy;
        }
        public override bool JudgeIsInControlRegion(MouseEventArgs e)
        {
            Rectangle rect = StaticFunction.GetControlRect(startPoint);
            Rectangle rect1 = new Rectangle(e.X, e.Y, 1, 1);
            if (rect.IntersectsWith(rect1))
            {
                whichControlRegion = 0;
                return true;
            }
            rect = StaticFunction.GetControlRect(middleLeftPoint);
            if (rect.IntersectsWith(rect1))
            {
                whichControlRegion = 1;
                return true;
            }
            rect = StaticFunction.GetControlRect(middleRightPoint);
            if (rect.IntersectsWith(rect1))
            {
                whichControlRegion = 2;
                return true;
            }
            rect = StaticFunction.GetControlRect(endPoint);
            if (rect.IntersectsWith(rect1))
            {
                whichControlRegion = 3;
                return true;
            }
            return false;
        }
        public override void ChangeSize(MouseEventArgs e)
        {
            int dx = e.X - this.MouseDownPoint.X;
            int dy = e.Y - this.MouseDownPoint.Y;
            this.MouseDownPoint = new Point(e.X, e.Y);
            if (whichControlRegion == 0)
            {
                this.startPoint = new Point(startPoint.X + dx, startPoint.Y + dy);
                if (isLeftEnabled == false)
                {
                    Point middlePoint = new Point((startPoint.X + endPoint.X) / 2, (startPoint.Y + endPoint.Y) / 2);
                    this.middleLeftPoint = new Point((startPoint.X + middlePoint.X) / 2, (startPoint.Y + middlePoint.Y) / 2);
                }
                if (isRightEnabled == false)
                {
                    Point middlePoint = new Point((startPoint.X + endPoint.X) / 2, (startPoint.Y + endPoint.Y) / 2);
                    this.middleRightPoint = new Point((endPoint.X + middlePoint.X) / 2, (endPoint.Y + middlePoint.Y) / 2);
                }
            }
            else if (whichControlRegion == 1)
            {
                this.middleLeftPoint.X = e.X;
                this.middleLeftPoint.Y = e.Y;
                this.isLeftEnabled = true;
            }
            else if (whichControlRegion == 2)
            {
                this.middleRightPoint.X = e.X;
                this.middleRightPoint.Y = e.Y;
                this.isRightEnabled = true;
            }
            else if (whichControlRegion == 3)
            {
                this.endPoint = new Point(endPoint.X + dx, endPoint.Y + dy);
                if (isLeftEnabled == false)
                {
                    Point middlePoint = new Point((startPoint.X + endPoint.X) / 2, (startPoint.Y + endPoint.Y) / 2);
                    this.middleLeftPoint = new Point((startPoint.X + middlePoint.X) / 2, (startPoint.Y + middlePoint.Y) / 2);
                }
                if (isRightEnabled == false)
                {
                    Point middlePoint = new Point((startPoint.X + endPoint.X) / 2, (startPoint.Y + endPoint.Y) / 2);
                    this.middleRightPoint = new Point((endPoint.X + middlePoint.X) / 2, (endPoint.Y + middlePoint.Y) / 2);
                }
            }
        }
        public override string ToString()
        {
            string printMsg = "[直线(" + this.startPoint.X + "," + this.startPoint.Y + ")]->[ID:" + this.Id + "]";
            return printMsg;
        }
        public override string GetDrawObjectMsg()
        {
            int angle = (int)(Math.Atan2(endPoint.Y - startPoint.Y, endPoint.X - startPoint.X) * 180 / Math.PI);
            string drawObjectMsg = "直线->起始位置(" + this.startPoint.X + "," + this.startPoint.Y + "),终点位置(" + this.endPoint.X + "," + this.endPoint.Y + "),水平偏角:" + angle + "°";
            return drawObjectMsg;
        }
        public override Rectangle GetRectangle()
        {
            return new Rectangle();
        }
        public override Point[] GetPoints()
        {
            return new Point[] { startPoint, endPoint };
        }
        public override bool IsAllInRectangle(Region region)
        {
            return (region.IsVisible(startPoint) && region.IsVisible(endPoint) && region.IsVisible(middleLeftPoint) && region.IsVisible(middleRightPoint));
        }
        public override DrawObject Clone(Point point)
        {
            return new DrawLine(point, point);
        }
        public override Point[] GetTextBoxLocation()
        {
            return new Point[] { startPoint };
        }
    }
}
